package ptoVenta;

import datechooser.beans.DateChooserDialog;
import java.sql.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author kirby joel del toro reyes
 */
public class cuentas extends javax.swing.JFrame {

    public cuentas() {
        initComponents();
        setExtendedState(ventas.MAXIMIZED_BOTH);
        jtxFecha1.requestFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jbtVerVentas = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbTotal = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jtxFecha2 = new javax.swing.JTextField();
        jtxFecha1 = new javax.swing.JTextField();
        jbtRegresar = new javax.swing.JButton();
        dateChooserCombo1 = new datechooser.beans.DateChooserCombo();
        dateChooserCombo2 = new datechooser.beans.DateChooserCombo();
        jbtFecha1 = new javax.swing.JButton();
        jbtFecha2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("VENTAS REALIZADAS: ");

        jbtVerVentas.setText("OK");
        jbtVerVentas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtVerVentasActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jtbTotal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jtbTotal);

        jLabel2.setText("FECHA");

        jbtRegresar.setText("REGRESAR A VENTAS");
        jbtRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRegresarActionPerformed(evt);
            }
        });

        jbtFecha1.setText("OK");
        jbtFecha1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtFecha1ActionPerformed(evt);
            }
        });

        jbtFecha2.setText("OK");
        jbtFecha2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtFecha2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtRegresar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 322, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 442, Short.MAX_VALUE)
                        .addComponent(jbtVerVentas))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jbtFecha1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxFecha1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dateChooserCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jbtFecha2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dateChooserCombo2, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jtxFecha2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxFecha2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxFecha1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtFecha1)
                    .addComponent(jbtFecha2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(dateChooserCombo2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(dateChooserCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtVerVentas)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtRegresar))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbtVerVentasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtVerVentasActionPerformed
// TODO add your handling code here:
    try {
        //Para establecer el modelo al JTable
        DefaultTableModel modelo = new DefaultTableModel();
        this.jTable1.setModel(modelo);
        //URL_bd = "jdbc:postgresql://192.168.0.252:5432/obras";
        try (Connection Conexion = DriverManager.getConnection("jdbc:postgresql://localhost:5432/punto_venta", "postgres", "12345")) {
            Statement s = Conexion.createStatement();
            ResultSet rs = s.executeQuery("SELECT id_producto,fecha_compra, SUM(producto_vendido) AS total, SUM(producto_vendido * precio_producto ) AS ganancia FROM venta WHERE fecha_compra BETWEEN '" + jtxFecha1.getText() + "' AND '" + jtxFecha2.getText() + "' group by id_producto, fecha_compra ORDER BY fecha_compra DESC;");
            //ResultSet rs = s.executeQuery("SELECT id_producto, SUM(cantidad_producto) AS total, SUM(cantidad_producto * precio_producto ) AS ganancia FROM venta group by id_producto;");
            ResultSetMetaData rsMd = rs.getMetaData();
            //La cantidad de columnas que tiene la consulta
            int cantidadColumnas = rsMd.getColumnCount();
            //Establecer como cabezeras el nombre de las colimnas
            for (int i = 1; i <= cantidadColumnas; i++) {
                modelo.addColumn(rsMd.getColumnLabel(i));
            }
            //Creando las filas para el JTable
            while (rs.next()) {
                Object[] fila = new Object[cantidadColumnas];
                for (int i = 0; i < cantidadColumnas; i++) {
                    fila[i] = rs.getObject(i + 1);
                }
                modelo.addRow(fila);
            }
            //Para establecer el modelo al JTable
            DefaultTableModel modelo2 = new DefaultTableModel();
            this.jtbTotal.setModel(modelo2);
            ResultSet rs2 = s.executeQuery("SELECT SUM(producto_vendido * precio_producto ) AS TOTAL FROM venta WHERE fecha_compra BETWEEN '" + jtxFecha1.getText() + "' AND '" + jtxFecha2.getText()+ "';");
            ResultSetMetaData rsMd2 = rs2.getMetaData();
            //La cantidad de columnas que tiene la consulta
            int cantiColumnas = rsMd2.getColumnCount();
            //Establecer como cabezeras el nombre de las colimnas
            for (int i = 1; i <= cantiColumnas; i++) {
                modelo2.addColumn(rsMd2.getColumnLabel(i));
            }
            //Creando las filas para el JTable
            while (rs2.next()) {
                Object[] filas = new Object[cantiColumnas];
                for (int i = 0; i < cantiColumnas; i++) {
                    filas[i] = rs2.getObject(i + 1);
                }
                modelo2.addRow(filas);
            }
            rs.close();
            rs2.close();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}//GEN-LAST:event_jbtVerVentasActionPerformed

private void jbtRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRegresarActionPerformed
// TODO add your handling code here:
    setVisible(false);
    ventas mod = new ventas();
    mod.setLocationRelativeTo(null);
    mod.setVisible(true);
}//GEN-LAST:event_jbtRegresarActionPerformed

private void jbtFecha1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtFecha1ActionPerformed
// TODO add your handling code here:
    DateChooserDialog dt = new DateChooserDialog();
    dt.showDialog(null);
    String fecha;
    fecha = dateChooserCombo1.getText();
    this.jtxFecha1.setText(fecha);
}//GEN-LAST:event_jbtFecha1ActionPerformed

private void jbtFecha2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtFecha2ActionPerformed
// TODO add your handling code here:
    DateChooserDialog dt2 = new DateChooserDialog();
    dt2.showDialog(null);
    String fecha2;
    fecha2 = dateChooserCombo2.getText();
    this.jtxFecha2.setText(fecha2);
}//GEN-LAST:event_jbtFecha2ActionPerformed

    public static void main(String argv[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(cuentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(cuentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(cuentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(cuentas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new cuentas().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private datechooser.beans.DateChooserCombo dateChooserCombo1;
    private datechooser.beans.DateChooserCombo dateChooserCombo2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbtFecha1;
    private javax.swing.JButton jbtFecha2;
    private javax.swing.JButton jbtRegresar;
    private javax.swing.JButton jbtVerVentas;
    private javax.swing.JTable jtbTotal;
    private javax.swing.JTextField jtxFecha1;
    private javax.swing.JTextField jtxFecha2;
    // End of variables declaration//GEN-END:variables
}